const app = getApp()
import { navBack, tipToast } from '../../../common/env';
import { createProductGroupModalMixin } from '../../../behaviors/product';
import { createProductGroup, deleteProductGroup, editProductGroup, getProductGroupList, sortProductGroup } from '../../../api/product';

Page({
    mixins: [createProductGroupModalMixin],
    options: {
        pureDataPattern: /^_/, // 指定所有 _ 开头的数据字段为纯数据字段
    },
    data: {
        activityGroupId: null,
        editMode: false,
    },
    onLoad(options) {
        this.options = options || {}
        options.value && this.setData({
            activityGroupId: options.value
        })
    },
    onReady() {
        this.movableAreaCom = this.selectComponent('#productGroupMovableArea');
    },
    // 切换至编辑模式
    changeToEditModal() {
        this.setData({
            editMode: true
        })
    },
    groupEditFinished() {
        const groupList = this.movableAreaCom.getGroupList().slice(0, -1)
        groupList.length && sortProductGroup({ productGroupIds: groupList.map(group => group.productGroupId) })
        app.event.trigger('productGroupListChange', groupList.map(item => ({ ...item })), true)

        this.setData({
            editMode: false
        })
    },
    // 分组点击选择
    handleGroupChoose(e) {
        const { productGroupId } = e.detail
        this.setData({
            activityGroupId: productGroupId
        }, () => {
            app.event.trigger('productGroupActiveTabChange', productGroupId);
            navBack();
        })
    },
    // 分组编辑
    async handleGroupEdit(e) {
        const { productGroupId, name, type } = e.detail
        let res = false
        switch (type) {
            case 'edit':
                res = await editProductGroup({
                    productGroupId,
                    name
                })
                if (res) {
                    const groupList = await getProductGroupList()
                    app.event.trigger('productGroupListChange', groupList)
                    this.movableAreaCom.groupEditRender({
                        productGroupId,
                        name
                    }, 'edit')
                }
                break;
            case 'delete':
                res = await deleteProductGroup({ productGroupId })
                if (res) {
                    const groupList = await getProductGroupList()
                    app.event.trigger('productGroupListChange', groupList)
                    this.movableAreaCom.groupEditRender({
                        productGroupId,
                    }, 'delete')
                }
                break;
            default:
                break;
        }
    },
    // 新建弹窗confirm
    async createGroup(e) {
        const value = e.detail
        if (value) {
            let res = createProductGroup({
                name: value
            })
            if (res) {
                const groupList = await getProductGroupList()
                app.event.trigger('productGroupListChange', groupList, true)
                this.movableAreaCom.groupEditRender(groupList.slice(-1)[0], 'create')
                this.setData({
                    createModalVisible: false
                })
            }
        } else {
            tipToast('请输入分组名称')
        }
    },
})
